<!--过滤器-->
<?php
$name = $age = $email = $url = $change =  '';
$ageErr =$emailErr= $urlErr = $changeErr= '';
    //提交表单后会触发这个校验
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $name = $_POST['fname'];
        $age = $_POST['age'];
        //filter_var() - 通过一个指定的过滤器来过滤单一的变量
        //FILTER_VALIDATE_INT	把值作为整数来验证。

        $ageErr = (!filter_var($age,FILTER_VALIDATE_INT))?'不要乱输兄各':'OK';
        $int_options = array(
            'options'=>array(
                'min_range' => 0,
                'max_range' => 250
            ));
        $ageErr = (filter_var($age,FILTER_VALIDATE_INT,$int_options))?'ok':'年龄太夸张了吧';
        $email = $_POST['email'];
        $emailErr = (filter_var($email,FILTER_VALIDATE_EMAIL))?'OK':"没有见过这么傻逼的邮箱";
        $url = $_POST['url'];
        $urlErr = filter_input(INPUT_POST,
            "url", FILTER_SANITIZE_URL);
        $change = $_POST['change'];
        $changeErr = str_replace('_','.',$change);
    }


?>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post">
    <label for="name">姓名</label>
    <input type="text" name="fname" id="name" value="<?php echo $name;?>"><br>
    <label for="age">年龄</label>
    <input type="text" name="age" id="age" value="<?php echo $age ;?>">
    <span><?php echo $ageErr;?></span>
    <br>
    <label for="email">邮箱</label>
    <input type="text" name="email" value="<?php echo $email; ?>">
    <span><?php echo $emailErr;?></span>
    <br>
    <label for="url">url净化</label>
    <input type="text" id="url" name="url" value="<?php echo $url;?>">
    <span><?php echo $urlErr;?></span>
    <br>
    <label for="change">字符转换</label>
    <input type="text" name="change" id="change" value = '<?php echo $change?>'>
    <span><?php echo $changeErr?></span>
    <br><br>
    <input type="submit" value="提交">
</form>

<?php

?>
